using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using Konst3d.Efficiency.Suite;

namespace Konst3d.Efficiency.Suite.SparsedBinary
{
    internal interface ITaskStorage
    {
        void UpdateTitle       (int id, string title);
        void UpdateDescription (int id, string description);
        void UpdateSeverity    (int id, TaskSeverity severity);
        void UpdateState       (int id, TaskState state);
        void UpdateDeadLine    (int id, DateTime deadLine);
    }

    internal class Task : ITask
    {
        #region ITask Members

        public int ID
        {
            get
            {
                return _id;
            }
        }

        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                _storage.UpdateTitle(_id, value);
                _title = value;
            }
        }

        public string Description
        {
            get
            {
                return _descr;
            }
            set
            {
                _storage.UpdateDescription(_id, value);
                _descr = value;
            }
        }

        public TaskSeverity Severity
        {
            get
            {
                return _severity;
            }
            set
            {
                _storage.UpdateSeverity(_id, value);
                _severity = value;
            }
        }

        public TaskState State
        {
            get
            {
                return _state;
            }
            set
            {
                _storage.UpdateState(_id, value);
                _state = value;
            }
        }

        public DateTime Created
        {
            get
            {
                return _created;
            }
        }

        public DateTime DeadLine
        {
            get
            {
                return _deadLine;
            }
            set
            {
                _storage.UpdateDeadLine(_id, value);
                _deadLine = value;
            }
        }

		public TimeSpan TimeLeft
		{
			get
			{
				switch (_state)
				{
					case TaskState.Active:
						return _deadLine > DateTime.Now ? _deadLine - DateTime.Now : new TimeSpan(0, 0, 0, 0, 0);

					default:
						return new TimeSpan(0, 0, 0, 0, 0);
				}
			}
		}

        #endregion

        #region Constructor

        public Task(ITaskStorage storage, int id, string title, string description, TaskSeverity severity, TaskState state, DateTime created, DateTime deadLine)
        {
            Contract.Requires(storage != null);

            _storage = storage;
            _id = id;
            _title = title;
            _descr = description;
            _severity = severity;
            _state = state;
            _created = created;
            _deadLine = deadLine;
        }

        #endregion // Constructor

        #region Fields

        private ITaskStorage _storage;
        private int _id;
        private string _title;
        private string _descr;
        private TaskSeverity _severity;
        private TaskState _state;
        private DateTime _created;
        private DateTime _deadLine;

        #endregion // Fields
    }
}
